package li.com.ruiji.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import li.com.ruiji.common.R;
import li.com.ruiji.entity.User;
import li.com.ruiji.service.UserService;
import li.com.ruiji.utils.SMSUtils;
import li.com.ruiji.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
@Api("用户登录")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;
    @ApiOperation("用户登录发送验证码")
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user,HttpSession session){
        String phone = user.getPhone();
        if(phone!=null){
//            生成验证码
            String integer = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("验证码是："+integer);
//            发短信(发短信的署名，模板CODE号，手机号，验证码)
//            SMSUtils.sendMessage("春树樱良","SMS_294036527",phone,integer.toString());
//            把验证码存到session中
//            session.setAttribute(phone,integer);
//            把验证码保存到redis中
            redisTemplate.opsForValue().set(phone,integer,5, TimeUnit.MINUTES);
            return R.success("验证码发送成功");
        }
        return R.success("验证码发送失败");
    }
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map,HttpSession session){
//        使用Map来接收前端传过来的数据，键为phone和code
//        获取手机号、验证码
        String phone = map.get("phone").toString();
        String code = map.get("code").toString();
//        从session中拿到验证码
//        Object attribute = session.getAttribute(phone);
//        从redis中拿到验证码
        Object attribute=redisTemplate.opsForValue().get(phone);
//        判断验证码是否正确
        if(code!=null&&phone!=null&&attribute.equals(code)){
//            判断该用户是否已注册
            LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(User::getPhone,phone);
            User one = userService.getOne(queryWrapper);
            if(one==null){
                one=new User();
                one.setPhone(phone);
                one.setStatus(1);
                userService.save(one);
            }
            session.setAttribute("user",one.getId());
//            登陆成功则删除储存中的验证码
            redisTemplate.delete(phone);
            return R.success(one);
        }
        return R.error("登录失败");
    }
    @PostMapping("/loginout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return R.success("退出成功");
    }
}
